﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tanks
{
    class AIcontroller : Controller
    {
        Game game = Game.Instance();
        Direction dir;
        public override void Control()
        {
            if (findTank&&!getBlock(dir))
            {
                tank.Fire();
            }
            else
            {
                int rnd = game.rand.Next(100);
                if (rnd < 10 && !getBlock(LeftTurn))
                {
                    dir = LeftTurn;
                }
                else if (rnd < 20 && !getBlock(RightTurn))
                {
                    dir = RightTurn;
                }
                else if (getBlock(dir))
                {
                    dir = randomDirection;
                }
                tank.Step(dir);
            }
        }
        public AIcontroller()
        {
            dir = randomDirection;
        }
        Direction LeftTurn
        {
            get
            {
                switch (dir)
                {
                    case Direction.North:
                        return Direction.West;
                    case Direction.South:
                        return Direction.East;
                    case Direction.West:
                        return Direction.South;
                    default:
                        return Direction.North;
                }
            }
        }
        Direction RightTurn
        {
            get
            {
                switch (dir)
                {
                    case Direction.North:
                        return Direction.East;
                    case Direction.South:
                        return Direction.West;
                    case Direction.West:
                        return Direction.South;
                    default:
                        return Direction.North;
                }
            }
        }
        bool getBlock(Direction Dir)
        {
            Cell front;
            switch (Dir)
            {
                case Direction.North:
                    if (tank.Y == 0)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X, tank.Y - 1];
                    break;
                case Direction.South:
                    if (tank.Y == game.FieldHeight - 1)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X, tank.Y + 1];
                    break;
                case Direction.West:
                    if (tank.X == 0)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X - 1, tank.Y];
                    break;
                default:
                    if (tank.X == game.FieldWidth - 1)
                    {
                        return true;
                    }
                    front = game.Grid[tank.X + 1, tank.Y];
                    break;
            }
            return front.Block != BlockType.Empty || front.currTank != null;
        }
        Direction randomDirection
        {
            get
            {
                switch (game.rand.Next(4))
                {
                    case 0: return Direction.North;
                    case 1: return Direction.South;
                    case 2: return Direction.West;
                    default: return Direction.East;
                }
            }
        }
        bool findTank
        {
            get
            {
                switch (dir)
                {
                    case Direction.North:
                        for (int i = tank.Y; i >= 0; i--)
                        {
                            if (game.Grid[tank.X, i].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[tank.X, i].currTank != null&&game.Grid[tank.X, i].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                        case Direction.West:
                        for (int i = tank.X; i >= 0; i--)
                        {
                            if (game.Grid[i, tank.Y].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[i, tank.Y].currTank != null && game.Grid[i, tank.Y].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                            case Direction.East:
                        for (int i = tank.X; i <game.FieldWidth; i++)
                        {
                            if (game.Grid[i, tank.Y].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[i, tank.Y].currTank != null && game.Grid[i, tank.Y].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;
                            case Direction.South:
                        for (int i = tank.Y; i < game.FieldHeight; i++)
                        {
                            if (game.Grid[tank.X, i].Block == BlockType.Solid)
                            {
                                return false;
                            }
                            if (game.Grid[tank.X, i].currTank != null && game.Grid[tank.X, i].currTank != this.tank)
                            {
                                return true;
                            }
                        }
                        break;                        
                }
                return false;
            }
        }
    }
}
